/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView;


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TOrder;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.page.PageDelegale;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.OrderHistoryModel;
import com.ruipengkj.service.TOrderService;
import com.ruipengkj.view.base.BasePanel;


/**历史订单
 * @Author：李智深
 * @Date：2012-03-14
 */
public class OrderHistoryPanel extends BasePanel implements Runnable,PageDelegale{

	private static final long serialVersionUID = 1L;
	private JLabelJTextFieldComponent orderNum; //订单号
	private JLabelJTextFieldComponent userNum; //用户编号
	private JLabelJTextFieldComponent seller; //销售人
	private DataPackageComponent startTime; //开始时间
	private DataPackageComponent endTime;	//结束时间
	
	private JTable table;
	private OrderHistoryModel orderOnlineModel;
	private JButton queryButton;//查询按钮
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TOrder> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TOrderService orderService;
	
	public OrderHistoryPanel(){
		super();
		this.setLayout(Util.BorderLayout());
		this.initView();
		this.initListener();
		orderService=BuildSpringApplication.getInstance().getTOrderServiceImpl();
	}
	
	private void initView(){
		pageView=new PageView<TOrder>();
		orderOnlineModel=new OrderHistoryModel();
		table=uiFactory.createJTable(orderOnlineModel, this, this, 30, JLabel.CENTER);
//		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableJsp=new JScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		this.add(topPanel(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(pagingComponent,BorderLayout.SOUTH);
		//排序
		TableRowSorter<OrderHistoryModel> tableRowSorter = new TableRowSorter<OrderHistoryModel>(orderOnlineModel);  
		table.setRowSorter(tableRowSorter);
	}
	private JPanel topPanel(){
		JPanel top_jp=new JPanel();
		top_jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		orderNum=new JLabelJTextFieldComponent("订单编号：", 10);
		userNum=new JLabelJTextFieldComponent("用户编号：", 10);
		seller=new JLabelJTextFieldComponent("销售人：", 10);
		startTime=new DataPackageComponent("开始时间：", 10);
		endTime=new DataPackageComponent("结束时间：", 10);
		queryButton=new JButton("查询");
		top_jp.add(orderNum);
		top_jp.add(userNum);
		top_jp.add(seller);
		top_jp.add(startTime);
		top_jp.add(endTime);
		top_jp.add(queryButton);
		return top_jp;
	}

	private void initListener(){
		queryButton.addActionListener(this);
		orderNum.getText().addKeyListener(this);
		userNum.getText().addKeyListener(this);
		seller.getText().addKeyListener(this);
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	//条件查询
	private void termQuery(int currentPage){
		TOrder torder=new TOrder();
		torder.setOrderID(orderNum.getText().getText().trim());
		TUser tuser=new TUser();
		tuser.setUserID(userNum.getText().getText().trim());
		torder.setTUser(tuser);
		torder.setOperator(seller.getText().getText().trim());
		torder.setHappenTime(startTime.getDate());
		torder.setEndTime(endTime.getDate());
		tableJsp.getViewport().remove(table);
		tableJsp.getViewport().add(waitComponent);
		try {
			pageView.setCurrentPage(currentPage);
			PageModel<TOrder> pageModel=orderService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),torder);
			pageView.setPageModel(pageModel);
			pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
			if(pageModel!=null){
				tableJsp.getViewport().remove(waitComponent);
				tableJsp.getViewport().add(table);
				orderOnlineModel.setRowData(pageModel.getResultList());
				table.updateUI(); 
				pagingComponent.show();
			}else{
				//没有订单数据
				waitComponent.setText("不存在数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
		}
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getSource()==orderNum.getText()||e.getSource()==userNum.getText()||e.getSource()==seller.getText()){
			//查询
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				queryButton.doClick();	
			}
		}
	}
	/**--------------------------------翻页查询接口------------------------------------*/
	//首页
	@Override
	public void homePage() {
		this.termQuery(1);
	}
	//尾页
	@Override
	public void lastPage() {
		this.termQuery((int)pageView.getTotalPage());
	}
	//下一页
	@Override
	public void nextPage() {
		if(pageView.getCurrentPage()>=pageView.getTotalPage()){
			this.termQuery((int)pageView.getTotalPage());
		}else{
			this.termQuery(pageView.getCurrentPage()+1);
		}
	}
	//上一页
	@Override
	public void prevPage() {
		if(pageView.getCurrentPage()<=1){
			this.termQuery(1);
		}else{
			this.termQuery(pageView.getCurrentPage()-1);
		}
	}

	@Override
	public String getInfo() {
		return "当前第 "+pageView.getCurrentPage()+" 页/共 " +pageView.getTotalPage()+" 页";
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}
